import Cesium from '../../Ces/Cesium'
/**
 *
 * @param {飞行航线} options
 */
function FlyingPath(options) {
  this._clampToGround = options.clampToGround??false
  this._outlineColor = Cesium.Color.WHITE;
  this._color = Cesium.Color.fromAlpha(this._outlineColor, 0.5);
  this._width = 12;
  this._iconSize = 16;
  this._heightReference = options.clamplToGround ? Cesium.HeightReference.CLAMP_TO_GROUND : Cesium.HeightReference.NONE;
  this._positions = options.positions;
  this._stationIds = options.stationIds;
  this._stationPosition = options.stationPosition;
  this._icon = options.icon;
  this._scene = options.scene;
  this._disableDepthTestDistance = Number.POSITIVE_INFINITY;
  this._dirty = true;
}

Object.defineProperties(FlyingPath.prototype, {
  iconSize: {
    get() {
      return this._iconSize;
    },
    set(value) {
      this._iconSize = value;
    },
  },
  width: {
    get() {
      return this._width;
    },
    set(value) {
      this._width = value;
    },
  },
  color: {
    get() {
      return this._color;
    },
    set(value) {
      this._color = value;
    },
  },
  outlineColor: {
    get() {
      return this._outlineColor;
    },
    set(value) {
      this._outlineColor = value;
    },
  },
});

FlyingPath.prototype.createPoint = function () {
  this._billboards = this._billboards && this._billboards.destroy();
  const iconSize = this.iconSize;
  const pixelOffset = new Cesium.Cartesian2(0, -iconSize * 0.2);
  const disableDepthTestDistance = this._disableDepthTestDistance;

  const ids = this._stationIds?? []
  const positions = this._stationPosition;
  const scene = this._scene;
  const image = this._icon;

  this._billboards = new Cesium.BillboardCollection({ scene });
  for (let i = 0; i < positions.length; i++) {
    const position = positions[i];

    this._billboards.add({
      id: i < ids.length ? ids[i] : undefined,
      position,
      height: iconSize,
      width: iconSize,
      image,
      heightReference: this._heightReference,
      pixelOffset,
      disableDepthTestDistance,
      scaleByDistance: this.scaleByDistance,
      pixelOffsetScaleByDistance: this.pixelOffsetScaleByDistance,
    });
  }
};

FlyingPath.prototype.createLine = function () {
  this._linePrimitive = this._linePrimitive && this._linePrimitive.destroy();
  const positions = this._positions;
  const width = this._width;

  const clampToGround = this._clampToGround;

  const instance = new Cesium.GeometryInstance({
    geometry: clampToGround ? new Cesium.GroundPolylineGeometry({
      positions,
      width,
    }) : new Cesium.PolylineGeometry({
      positions,
      width,
    }),
  });

  const appearance = new Cesium.PolylineMaterialAppearance({
    material: Cesium.Material.fromType(Cesium.Material.PolylineOutlineType),
  });

  const that = this;

  appearance.material._uniforms.color_0 = function () {
    return that._color;
  };
  appearance.material._uniforms.outlineColor_1 = function () {
    return that._outlineColor;
  };
  appearance.material.uniforms.outlineWidth = 4.0;
  appearance.material.uniforms.outlineColor = Cesium.Color.WHITE;

  this._linePrimitive = clampToGround ? new Cesium.GroundPolylinePrimitive({
    geometryInstances: instance,
    appearance,
    arcType: Cesium.ArcType.NONE,
  })
    : new Cesium.Primitive({
      geometryInstances: instance,
      appearance,
      arcType: Cesium.ArcType.NONE,
    });
};


FlyingPath.prototype.destroy = function () {
  this._linePrimitive = this._linePrimitive && this._linePrimitive.destroy();
  this._billboards = this._billboards && this._billboards.destroy();

  return Cesium.destroyObject(this);
};

FlyingPath.prototype.isDestroyed = function () {
  return false;
};

FlyingPath.prototype.update = function (frameState) {
  if (this._dirty) {
    this._dirty = false;

    this.createLine();
    this.createPoint();
  }
  this._billboards.update(frameState);
  this._linePrimitive.update(frameState);
};

export default FlyingPath;
